<?php 
	require_once("models/config.php");
	require_once("ui.php");
	require_once("stats/db.php");
	
	if(!isUserLoggedIn()) {
		header ("location: login.php");
		die();
	}

	global $loggedInUser;
	$isAdmin = $loggedInUser->isAdministrator();
	if ($isAdmin) {
		die();		
	}
?>

<?php 

//Forms posted
if(!empty($_POST)) {
	global $reportPostErrors;
	$reportPostErrors = array();
	
	$error = processPost($loggedInUser);
	if ($error != null) {
		array_push($reportPostErrors, $error);
	}
}

function processPost() {
	global $loggedInUser;
	
	$post = readPost("develTime");
	$parsedTime = parseDebuggingTime($post);
	if ($parsedTime['error'] != "") {
		return $parsedTime['error'];
	}

	$dbTime = mysql_real_escape_string($parsedTime['time']);
	
	$user = new UserStats($loggedInUser->user_id);
	$user->updateDevelopmentTime($dbTime);
	
	return $user->error;
}

function printHeader() {
	global $reportPostErrors;
	
	if (isset($reportPostErrors)) {
		if (count($reportPostErrors) == 0) {
			echo '<meta HTTP-EQUIV="REFRESH" content="0; url=' . $_SERVER['PHP_SELF'] . '?success=true">';
			return;		
		}
	}
}

?>
		
<?php printPageHead("", "printHeader"); ?>

	<?php printNavigationBar('index', NAVIGATION_ITEM_FILE_BUG) ?>		
		
		<div id="content" class="container_16 clearfix">
			<form name="additionalDataForm" action="<?php echo $_SERVER['PHP_SELF'] . "#result-anchor"; ?>" method="post">
                <p>
                    <label for="develTime">Total development time (e.g. 6h; 2h 30m; 50m; 0m; ...):</label>
                    
                    <?php 
                    	$shownText = "";

                    	$unsuccessfulPost = isset($reportPostErrors) && (count($reportPostErrors) > 0);
                    	
                    	if ($unsuccessfulPost) {
                    		$shownText = getOriginalPost("develTime");
                    	} else {
                    		$user = new UserStats($loggedInUser->user_id);
                    		$user->loadStats();
                    		
                    		$shownText = convertDbTimeToString($user->develTime);
                    	}
                    	
                    	echo '<input type="text" name="develTime" value="' . $shownText . '" />';
                    ?>
                </p>				
					
				<div>
					<div class="aloneFormButton" ><input type="submit" value="Submit" class="submit" /></div>
				</div>				
			</form>

			<div id="reportSubmissionResultDiv">
				<a id="result-anchor"></a>
			<?php 
				if (isset($reportPostErrors)) {
					if (count($reportPostErrors) > 0) {
						echo '<span id="reportSubmissionFailureTitle">Data not submitted</span><br>';
						
						echo '<ul id="reportSubmissionResultlist">';
						foreach ($reportPostErrors as $i => $error) {
							echo "<li>$error</li>" . PHP_EOL;
						}
						
						echo '</ul>';
					}
				}
				
				if (isset($_GET['success'])) {
					if ($_GET['success'] == "true") {
						echo '<span id="reportSubmissionSuccessTitle">Data submitted</span><br>';
					}
				}
			?>
			</div>
		</div>
 
<?php printPageFoot(); ?>
